package com.example.suntest.service3;

import com.example.suntest.entity.User;
import com.example.suntest.entity.UserDTO;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @version 1.0
 * @Package: com.example.suntest.service3
 * @ClassName: UserToUserDTOConverter
 * @Author: sgq
 * @Date: 2024/7/11 9:28
 * @Description: TODD
 */
@Component
public class UserToUserDTOConverter implements Converter<User, UserDTO> {
    @Override
    public UserDTO convert(User source) {
        if (source == null) {
            return null;
        }
        UserDTO target = new UserDTO();
        target.setId(source.getId());
        target.setName(source.getName());
        target.setPassword(source.getPassword());
        return target;
    }

    @Override
    public List<UserDTO> batchConvert(List<User> sources) {
        if (sources == null) {
            return Collections.emptyList();
        }
        List<UserDTO> targets = new ArrayList<>();
        for (User source : sources) {
            targets.add(convert(source));
        }
        return targets;
    }
}